﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Squadron.Widgets
{
    public class WidgetManager
    {
        private Color Color1 = Color.WhiteSmoke;
        private Color Color2;

        private Color Color1Dark = Color.FromArgb(63, 72, 204);
        private Color Color2Dark;

        public WidgetManager()
        {
            int diff = 50;

            Color2 = Color.FromArgb(Color1.R - diff, Color1.G - diff, Color1.B - diff);
            Color2Dark = Color.FromArgb(Color1Dark.R - diff, Color1Dark.G - diff, Color1Dark.B - diff);
        }

        public void InitializeControls(Control container)
        {
            if (container.Tag == null)
                foreach (Control control in SquadronContext.Utils.GetControlsRecursively(container))
                {
                    if (control is Button)
                    {
                        if ((control as Button).BackgroundImage == null)
                        {
                            (control as Button).Paint += Background_Paint;
                            (control as Button).BackgroundImage = DrawingResources.BG1;
                            (control as Button).ForeColor = Color.White;
                        }
                    }

                    else if (control is Panel)
                    {
                        if ((control as Panel).BackgroundImage == null)
                        {
                            (control as Panel).Paint += Background_Paint;
                        }
                    }

                    else if (control is DataGridView)
                    {
                        DataGridView grid = control as DataGridView;

                        grid.BackgroundColor = Color.LightSteelBlue;
                        SetContextMenuStrip(grid);
                    }

                    else if (control is CheckedListBox)
                    {
                        CheckedListBox cbx = control as CheckedListBox;

                        SetContextMenuStrip(cbx);
                    }
                }

            container.Tag = "Init-Done";
        }

        private void SetContextMenuStrip(CheckedListBox cbx)
        {
            if (cbx.ContextMenuStrip == null)
            {
                cbx.ContextMenuStrip = new ContextMenuStrip();
            } 

            if (!cbx.ContextMenuStrip.Items.Cast<ToolStripMenuItem>().Any(m => m.Text == "Check All"))
            {
                ToolStripItem item = cbx.ContextMenuStrip.Items.Add("Check All");
                item.Click += delegate(object s, EventArgs e)
                {
                    CheckedListBox control = (s as ToolStripItem).Tag as CheckedListBox;
                    for (int i = 0; i < control.Items.Count; i++)
                        control.SetItemChecked(i, true);
                };

                item.Tag = cbx;
            }

            if (!cbx.ContextMenuStrip.Items.Cast<ToolStripMenuItem>().Any(m => m.Text == "Uncheck All"))
            {
                ToolStripItem item = cbx.ContextMenuStrip.Items.Add("Uncheck All");
                item.Click += delegate(object s, EventArgs e)
                {
                    CheckedListBox control = (s as ToolStripItem).Tag as CheckedListBox;
                    for (int i = 0; i < control.Items.Count; i++)
                        control.SetItemChecked(i, false);
                }; 
                item.Tag = cbx;
            }
        }

        private void SetContextMenuStrip(DataGridView grid)
        {
            if (grid.ContextMenuStrip == null)
            {
                grid.ContextMenuStrip = new ContextMenuStrip();
            }

            if (!grid.ContextMenuStrip.Items.Cast<ToolStripMenuItem>().Any(m => m.Text == "Import Data"))
            {
                ToolStripItem item = grid.ContextMenuStrip.Items.Add("Import Data");
                item.Click += new EventHandler(ImportData_Click);
                item.Tag = grid;
            }

            if (!grid.ContextMenuStrip.Items.Cast<ToolStripMenuItem>().Any(m => m.Text == "Export Data"))
            {
                ToolStripItem item = grid.ContextMenuStrip.Items.Add("Export Data");
                item.Click += new EventHandler(ExportData_Click);
                item.Tag = grid;
            }
        }

        void ExportData_Click(object sender, EventArgs e)
        {
            SquadronContext.Utils.ExportToCSV((sender as ToolStripItem).Tag as Control, null, true);
        }

        void ImportData_Click(object sender, EventArgs e)
        {
            SquadronContext.Utils.ImportFromCSV((sender as ToolStripItem).Tag as Control);
        }

        void Background_Paint(object sender, PaintEventArgs e)
        {
            // Background
            Color c1 = Color1;
            Color c2 = Color2;

            if (sender is Button)
            {
            }
            else
                DrawBackground(e.Graphics, (sender as Control).ClientRectangle, c1, c2);

            // Text
            if (sender is Button)
            {
            }
        }

        private void DrawImage(object sender)
        {
            (sender as Button).BackgroundImage = DrawingResources.BG1;
            (sender as Button).Invalidate();
        }

        public void DrawBackground(Graphics g, Rectangle rectangle, Color c1, Color c2)
        {
            LinearGradientBrush br = new LinearGradientBrush(rectangle, Color.Black, Color.Black, 0, false);
            ColorBlend cb = new ColorBlend();
            cb.Positions = new[] { 0, 0.3f, 1 };
            cb.Colors = new[] { c1, c2, c1 };
            br.InterpolationColors = cb;
            // rotate
            br.RotateTransform(90);
            // paint
            g.FillRectangle(br, rectangle);
        }

        private Brush _textBrush = new SolidBrush(Color.White);
        private Font _textFont = new Font("Calibri", 11, FontStyle.Bold);

        private void DrawText(string text, PaintEventArgs e)
        {
            PointF pointF = new PointF();
            pointF.X = (e.ClipRectangle.Width - e.Graphics.MeasureString(text, _textFont).Width) / 2;
            pointF.Y = (e.ClipRectangle.Height - e.Graphics.MeasureString(text, _textFont).Height) / 2;

            e.Graphics.DrawString(text, _textFont, _textBrush, pointF);
        }
    }
}
